#include "serializationjsontest.h"
#include <QDebug>
#include <common/logger/logger.h>

#ifdef Q_OS_WIN
#include <windows.h>
#include <winnls.h>
#endif

using namespace std;
using namespace xlist;
using namespace serialization;
SerializationJsonTest::SerializationJsonTest(QObject* parent)
	: QObject(parent)
{
#ifdef Q_OS_WIN
	// 控制台输出 UTF-8
	// SetConsoleOutputCP(CP_UTF8);
#endif
	// init value
	m_data.size = 10000;
	m_data.num = 95501;
	m_data.type = 0;
	m_data.score = 95.80;
	m_data.desc = "TestData..";
	m_data.date = make_optional(QDateTime::fromString("2025-09-01 16:30:35", "yyyy-MM-dd HH:mm:ss"));
	m_data.lsdata = XList<int>({1, 2, 3, 4, 5});
	m_data.var = "varTest";
	LOG_SET_CONFIG_PATH("./logCof.yaml");
	LOG_WARN("测试");

	m_jsonstr.clear();
	m_jsonstr2 = R"({
		"nnn":"789",
		"fff":"123.45",
		"sss":456.101,
		"kkk":"ignore"
	})";
	m_jsonstr3 = R"({"success":false,"code":"10004","msg":"文件不能为空","data":null})";
}

SerializationJsonTest::~SerializationJsonTest()
{
}

void SerializationJsonTest::initTestCase()
{
	qDebug() << "CLASS:SerializationJsonTest START";
}

void SerializationJsonTest::tstSerialize()
{
	m_jsonstr = serializeJson(m_data);
	QVERIFY(!m_jsonstr.empty());
	qDebug() << "json str:" << m_jsonstr.c_str();
}

void SerializationJsonTest::tstDeserialize()
{
	QVERIFY(!m_jsonstr.empty());
	StQtData data;
	bool rst = deserializeJson(m_jsonstr, data);
	QVERIFY(rst);
	data.print();
	QCOMPARE(m_data, data);

	QVERIFY(!m_jsonstr2.empty());
	StTest test;
	rst = deserializeJson(m_jsonstr2, test);
	QVERIFY(rst);
	test.print();

	QVERIFY(!m_jsonstr3.empty());
	UploadResult uploadRst;
	rst = deserializeJson(m_jsonstr3, uploadRst);
	QVERIFY(rst);
	uploadRst.print();
}

void SerializationJsonTest::cleanupTestCase()
{
	qDebug() << "CLASS:SerializationJsonTest END";
}

int main(int argc, char** argv)
{
	SerializationJsonTest tc;
	return QTest::qExec(&tc, argc, argv);// qExec 会把失败信息打印到 stdout
}